package cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.birthday;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SnapHelper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.matter.MatterApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.matter.MatterBirthday;
import cn.shyouchi.wuguanjia.android.widget.GlideCircleTransform;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ChooseGiftActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_gift_list)
    RecyclerView mRvGiftList;
    @BindView(R.id.tv_person_name)
    TextView mTvPersonName;
    @BindView(R.id.btn_send_gift)
    Button mBtnSendGift;


    List<BirthdayGift> mBirthdayGifts = new ArrayList<>();
    BirthdayGiftAdapter mBirthdayGiftAdapter;


    MatterApi mMatterApi;

    MatterBirthday mMatterBirthday;
    @BindView(R.id.edit_reason)
    EditText mEditReason;
    @BindView(R.id.img_header)
    ImageView mImgHeader;

    @Override
    public int initContentView() {
        return R.layout.activity_choose_gift;
    }

    @Override
    public void initInjector() {
        mMatterBirthday = getIntent().getParcelableExtra("birthday");
        mMatterApi = new MatterApi();

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        for (int i = 0; i < 200; i++) {
            mBirthdayGifts.add(new BirthdayGift(4001, R.mipmap.ic_gift_case1, "巧克力蛋糕"));
            mBirthdayGifts.add(new BirthdayGift(4002, R.mipmap.ic_gift_case2, "草莓蛋糕"));
            mBirthdayGifts.add(new BirthdayGift(4003, R.mipmap.ic_gift_case3, "奶油蛋糕"));
        }


        mRvGiftList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                int pos = mRvGiftList.getChildAdapterPosition(centerView());
                mBirthdayGiftAdapter.setSelected(pos);
                mBirthdayGiftAdapter.notifyDataSetChanged();

            }
        });

        mBirthdayGiftAdapter = new BirthdayGiftAdapter(mBirthdayGifts) {
            @Override
            public void onItemClick(BaseViewHolder holder, BirthdayGift entity) {
                int pos = mRvGiftList.getChildAdapterPosition(centerView());
                int cur = holder.getPosition();
                if (cur < pos) {
                    mRvGiftList.smoothScrollToPosition(cur > 0 ? cur - 1 : 0);
                } else if (cur == pos) {

                } else if (cur > pos) {
                    mRvGiftList.smoothScrollToPosition(cur + 1);
                }
                mBirthdayGiftAdapter.notifyDataSetChanged();
            }
        };

        mRvGiftList.setAdapter(mBirthdayGiftAdapter);
        mRvGiftList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));

        mBirthdayGiftAdapter.setEnableLoadMore(false);

        mBirthdayGiftAdapter.setSelected(mBirthdayGifts.size() / 2);
        mRvGiftList.scrollToPosition(mBirthdayGifts.size() / 2);

        SnapHelper snapHelperCenter = new LinearSnapHelper();
        snapHelperCenter.attachToRecyclerView(mRvGiftList);

        mTvPersonName.setText(mMatterBirthday.getName());
        loadHead(Global.BASE_URL + mMatterBirthday.getAvatar(), mImgHeader);

        mBtnSendGift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendGift();
            }
        });

    }


    private View leftView() {
        int w = mRvGiftList.getMeasuredWidth() / 6;
        int h = mRvGiftList.getMeasuredHeight() / 2;
        return mRvGiftList.findChildViewUnder(w, h);
    }

    private View centerView() {
        int w = mRvGiftList.getMeasuredWidth() / 6 * 3;
        int h = mRvGiftList.getMeasuredHeight() / 2;
        return mRvGiftList.findChildViewUnder(w, h);
    }

    private View rightView() {
        int w = mRvGiftList.getMeasuredWidth() / 6 * 5;
        int h = mRvGiftList.getMeasuredHeight() / 2;
        return mRvGiftList.findChildViewUnder(w, h);
    }


    private void sendGift() {

        BirthdayGift gift = mBirthdayGiftAdapter.getSelectedEntity();

        mMatterApi.sendGift(String.valueOf(mMatterBirthday.getId()), gift.getId(), mEditReason.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseResult>(mContext) {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        EventBus.getDefault().post(new DataChangeEvent());
                        finish();
                    }
                });
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void loadHead(String path, ImageView imageView) {
        Glide.with(mContext)
                .load(path)
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }


}
